//
// Created by 梦小星 on 2021/10/25.
// @author 梦小星 https://mengxiaoxing.top
// @mail mengxingcn@aliyun.com
//
#include "SqList.h"
#include <iostream>
using namespace std;
/*
 * 算法思想；先将数组A【m+n】中的所有元素逆置，再对前n个元素逆置，最后把后m个元素逆置
 * */
/*
 * 逆置指定位置的之间的元素
 * */
void reversal(int a[],int left,int right,int arraySize){
    if (right<=left||right>=arraySize) return;
    for (int i = 0; i <= (right+left)/2-left; ++i) {
        int tmp =a[left+i];
        a[left+i]=a[right-i];
        a[right-i]=tmp;
    }
}
void exchange(int a[],int m,int n,int arraySize){
    /*
     * 数组[m+n]中，从0到m-1个元素存放顺序表a，m到m+n-1存放顺序表b，算法将这两个表互换*/
    reversal(a,0,m+n-1,arraySize);
    reversal(a,0,n-1,arraySize);
    reversal(a,n,m+n-1,arraySize);
}
int main(){
    SqList a,b;
    initList(a);
    initList(b);
    for (int i = 0; i < 10; ++i) {
        listInsert(a,i+1,i);
    }
    for (int i = 0; i <10; ++i) {
        listInsert(b,i+1,10-i);
    }
    cout << "顺序表a";
    printList(a);
    cout << "顺序表b";
    printList(b);
    int array[20]={},m=a.length,n=b.length;

    cout << endl;
    for (int i = 0; i < m; ++i) {
        array[i]=a.data[i];
    }
    for (int i = 0; i < n; ++i) {
        array[i+m]=b.data[i];
    }
    cout << "数组";
    for (int i = 0; i < 20; ++i) {
        cout << array[i];
    }

    cout << endl;
    exchange(array,m,n,20);
    cout << "数组";
    for (int i = 0; i < 20; ++i) {
        cout << array[i];
    }

    cout << endl;
}
